package com.platform.api;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.entity.ActivityRecordVo;
import com.platform.entity.ActivityVo;
import com.platform.entity.CommonwealVo;
import com.platform.entity.UserVo;
import com.platform.service.ApiActivityRecordService;
import com.platform.service.ApiActivityService;
import com.platform.util.ApiBaseAction;
import com.platform.util.ApiPageUtils;
import com.platform.utils.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/activity")
public class ApiActivityController extends ApiBaseAction {

    @Autowired
    private ApiActivityService activityService;
    @Autowired
    private ApiActivityRecordService activityRecordService;

    @RequestMapping(value = "signUp", method = {RequestMethod.GET, RequestMethod.POST})
    public Object signUp(@LoginUser UserVo loginUser,
                         @RequestParam(value = "id") Integer id,
                         @RequestParam(value = "name") String name,
                         @RequestParam(value = "sex") Integer sex,//1-男；2-女
                         @RequestParam(value = "mobile") String mobile,
                         String remark) {

        Long userId = loginUser.getUserId();

        //todo 参数校验
        if (!Validator.isMobile(mobile)) {
            return toResponsFail("请填写正确的手机号码");
        }
        if (name.length() > 20) {
            return toResponsFail("姓名不能超过20个字符");
        }
        if (remark != null && remark.length() > 100) {
            return toResponsFail("留言不能超过100个字符");
        }


        //todo 查询活动状态
        ActivityVo activityVo = activityService.queryObject(id);
        if (activityVo == null) {
            return toResponsFail("活动不存在");
        }
        if (activityVo.getIsFinish() == 1) {
            return toResponsFail("活动已结束");
        }
        if (DateUtil.isExpired(new Date(), DateField.SECOND, 1, activityVo.getActivityEndTime())) {
            return toResponsFail("活动已结束");
        }

        //todo 查询是否有报名
        Map<String, Object> params = new HashMap<>();
        params.put("activityId", id);
        params.put("userId", userId);
        int i = activityRecordService.queryTotal(params);
        if (i > 0) {
            return toResponsFail("请勿重复报名");
        }

        //todo 保存
        ActivityRecordVo activityRecordVo = new ActivityRecordVo();
        activityRecordVo.setUserId(userId.intValue());
        activityRecordVo.setActivityId(id);
        activityRecordVo.setActivityName(activityVo.getTitle());
        activityRecordVo.setName(name);
        activityRecordVo.setSex(sex);
        activityRecordVo.setMobile(mobile);
        activityRecordVo.setRemark(remark);
        activityRecordVo.setAddTime(new Date());
        activityRecordService.save(activityRecordVo);

        return toResponsSuccess("");
    }


    /**
     * @param loginUser
     * @param page
     * @param size
     * @param type      0-全部；1-我已经报名的活动
     * @return
     */
    @RequestMapping(value = "list", method = {RequestMethod.GET, RequestMethod.POST})
    public Object list(@LoginUser UserVo loginUser,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "size", defaultValue = "10") Integer size,
                       @RequestParam(value = "type", defaultValue = "0") Integer type) {
        Long userId = loginUser.getUserId();
        Map<String, Object> param = new HashMap<>();
        param.put("page", page);
        param.put("limit", size);
        param.put("sidx", "id");
        param.put("order", "desc");
        if (type == 1) {//1-我已经报名的活动
            param.put("userId", userId);
        }

        Query query = new Query(param);
        List<ActivityVo> activityVos = activityService.queryList(query);
        int total = activityService.queryTotal(query);

        //todo 判断活动是否已经结束
        if (activityVos.size() > 0) {
            for (ActivityVo activityVo : activityVos) {
                if (activityVo.getIsFinish() == 0) {//只判断isFinish等于0的情况下
                    activityService.updateExpiredActivity(activityVo);
                    //todo 是否已经报名
                    Map<String, Object> params = new HashMap<>();
                    params.put("activityId", activityVo.getId());
                    params.put("userId", userId);
                    int i = activityRecordService.queryTotal(params);
                    if (i > 0) {
                        activityVo.setIsSignUp(1);
                    }
                }
            }
        }


        ApiPageUtils pageUtil = new ApiPageUtils(activityVos, total, query.getLimit(), query.getPage());

        return toResponsSuccess(pageUtil);
    }

    @RequestMapping(value = "detail", method = {RequestMethod.GET, RequestMethod.POST})
    public Object detail(@LoginUser UserVo loginUser,
                         @RequestParam(value = "id") Integer id) {
        ActivityVo activityVo = activityService.queryObject(id);
        if (activityVo == null) {
            return toResponsFail("不存在该活动");
        }
        if (activityVo.getIsFinish() == 0) {//只判断isFinish等于0的情况下
            activityService.updateExpiredActivity(activityVo);
        }

        //todo 是否已经报名
        Map<String, Object> params = new HashMap<>();
        params.put("activityId", activityVo.getId());
        params.put("userId", loginUser.getUserId());
        int i = activityRecordService.queryTotal(params);
        if (i > 0) {
            activityVo.setIsSignUp(1);
        }
        return toResponsSuccess(activityVo);
    }


}
